/*******************************************************************************
 * Simple C-K Editor
 * 
 * Copyright (C) 2009 Ingi BROWN - Centre de Gestion Scientifique - Ecole des Mines ParisTech
 * Licensed under the GNU General Public License Version 3.
 * 
 * Simple C-K Editor includes source code and libraries from the JUNG Project (Java Universal Network/Graph Framework) 
 * released under the BSD license. Copyright (c) 2003-2004,  Regents of the University of California and the JUNG Project.
 * 
 * See the License.txt file for more details.
 ******************************************************************************/

package edu.ensmp.ckEditor.gui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import edu.ensmp.ckEditor.main.Launcher;
import edu.ensmp.ckEditor.util.Constants;


@SuppressWarnings("serial")
public class MenuBar extends JMenuBar implements ActionListener {
	
	final private JMenuItem savePNGItem;
	final private JMenuItem saveGraphItem;

	public MenuBar() {

		JMenu actionMenu = new JMenu("Actions");
		// Change the size so I can see the text
		actionMenu.setPreferredSize(new Dimension(150, 20));

		/*
		 * Save to PNG Item
		 */
		savePNGItem = new JMenuItem("Export graph");
		actionMenu.add(savePNGItem);

		/*
		 * Save to PNG Item
		 */
		saveGraphItem = new JMenuItem("Save Graph");
		actionMenu.add(saveGraphItem);

		// Add the menu to the container
		add(actionMenu);

		// Add the action listeners
		savePNGItem.addActionListener(this);
		saveGraphItem.addActionListener(this);

	}

	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == savePNGItem) {
			
			JFileChooser fc = new JFileChooser();
			File file;
			
			// Save As Dialog
			int returnVal = fc.showSaveDialog(this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
            } else {
                // This is if we don't give a valid filename, save by default in application folder.
            	file = new File(Constants.PNG_DEFAULT_FILEPATH);
            }
			
			// Screenshot method
			Point frameLocation = Launcher.getEditorGUI().getEditorFrame()
					.getLocation();
			Dimension windowSize = Launcher.getEditorGUI().getEditorFrame()
					.getSize();
			Rectangle rect = new Rectangle(frameLocation.x,
					frameLocation.y + 50, windowSize.width,
					windowSize.height - 86);
			try {
				Robot robot;
				robot = new Robot();

				BufferedImage image = robot.createScreenCapture(rect);		

				// Save the screenshot as a png
				ImageIO.write(image, "png", file);
                
			} catch (Exception exception) {
				exception.printStackTrace();
			}
		}

		if (event.getSource() == saveGraphItem) {
			try {
				Launcher.getLogger().SaveToFile("History.txt");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
